﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters.Binary;

using System.IO;
using System.Data.Common;
using System.Data;

namespace Huazhu.PMS.Framework.Organization
{
    public class OrgTreeNode
    {
        public static readonly string ROOT = "root:";
        private string _id;
        private SubNodeCollection _subNodes;

        public SubNodeCollection SubNodes
        {
            get { return _subNodes; }
        }

        Dictionary<string, Type> notificationTypes = new Dictionary<string, Type>();

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _areaId;

        public string AreaId
        {
            get { return _areaId; }
            set { _areaId = value; }
        }

        private string _nodeType;

        public string NodeType
        {
            get { return _nodeType; }
            set { _nodeType = value; }
        }

        public string HotelProperty
        {
            get;
            set;
        }

        private string _bizScope;

        /// <summary>
        /// 经营范围（内宾外宾）
        /// </summary>
        public string BizScope
        {
            get { return _bizScope; }
            set { _bizScope = value ?? string.Empty; }
        }

        private string address;

        public string Address { get { return address; } set { address = value; } }

        private string phoneNumber;

        public string PhoneNumber { get { return phoneNumber; } set { phoneNumber = value; } }

        private string fax;

        public string Fax { get { return fax; } set { fax = value; } }

        private string zip;

        public string Zip { get { return zip; } set { zip = value; } }

            /// <summary>
        /// 本节点名称
        /// </summary>
        private string _nodeName;

        /// <summary>
        /// 节点名称
        /// </summary>
        public string NodeName
        {
            get
            {
                return _nodeName;
            }
            set
            {
                _nodeName = value;
            }
        }

        /// <summary>
        /// 节点等级
        /// </summary>
        private string _level;

        /// <summary>
        /// 节点等级
        /// </summary>
        public string Level
        {
            get { return _level; }
            set { _level = value; }
        }
        
        /// <summary>
        /// 父节点对象
        /// </summary>
        private OrgTreeNode parentNode;

        public OrgTreeNode ParentNode
        {
            get { return parentNode; }
            set
            {
                parentNode = value;
            }
        }

        /// <summary>
        /// 返回节点在组织结构树上的绝对路径
        /// </summary>
        public string FullName
        {
            get 
            {
                if (parentNode != null)
                    return parentNode.FullName + "/" + _nodeName;

                return _nodeName;
            }
        }

        /// <summary>
        /// 默认构造函数，节点默认名称为"root:"
        /// </summary>
        internal OrgTreeNode()
        {
            _nodeName = ROOT;

            _subNodes = new SubNodeCollection(this);
        }

        /// <summary>
        /// 带名称的构造函数
        /// </summary>
        /// <param name="parentNode">父节点对象</param>
        /// <param name="name">节点名称</param>
        public OrgTreeNode(OrgTreeNode parentNode, string name)
        {
            this.parentNode = parentNode;
            _nodeName = name;
        }

        private IDictionary<string, OrgTreeNode> childNodes = new Dictionary<string, OrgTreeNode>();

        /// <summary>
        /// 获取子节点枚举器，节点名对节点对象
        /// </summary>
        public IEnumerable<KeyValuePair<string, OrgTreeNode>> ChildNodes
        {
            get { return childNodes; }
        }

        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="childNode">子节点对象</param>
        public void Append(OrgTreeNode childNode)
        {
            lock (this)
            {
                if (childNodes.ContainsKey(childNode._nodeName))
                {
                   throw new InvalidOperationException("Already has the node:" + childNodes[childNode._nodeName].FullName);
                }

                System.Threading.Thread.MemoryBarrier();

                childNodes.Add(childNode._nodeName, childNode);

                childNode.parentNode = this;
                
            }
        }

        /// <summary>
        /// 移除子节点
        /// </summary>
        /// <param name="childNode">子节点对象</param>
        public void Remove(OrgTreeNode childNode)
        {
            lock (this)
            {
                if (childNodes.ContainsKey(childNode._nodeName))
                {
                    childNodes.Remove(childNode._nodeName);
                    childNode.parentNode = null;
                }
            }
        }

        

        public static bool operator > (OrgTreeNode nodeOne, OrgTreeNode nodeTwo)
        {
            if (nodeOne == null || nodeTwo == null)
                return false;

            OrganizationScope scopeOne = new OrganizationScope(nodeOne);

            OrganizationScope scopeTwo = new OrganizationScope(nodeTwo);

            return scopeOne > scopeTwo;
        }

        public static bool operator <(OrgTreeNode nodeOne, OrgTreeNode nodeTwo)
        {
            if (nodeOne == null || nodeTwo == null)
                return false;

            OrganizationScope scopeOne = new OrganizationScope(nodeOne);

            OrganizationScope scopeTwo = new OrganizationScope(nodeTwo);

            return scopeOne < scopeTwo;
        }

        public static bool operator >= (OrgTreeNode nodeOne, OrgTreeNode nodeTwo)
        {
            if (nodeOne == null || nodeTwo == null)
                return false;

            return nodeOne > nodeTwo || nodeOne == nodeTwo;
        }

        public static bool operator <=(OrgTreeNode nodeOne, OrgTreeNode nodeTwo)
        {
            if (nodeOne == null || nodeTwo == null)
                return false;

            return nodeOne < nodeTwo || nodeOne == nodeTwo;
        }
    }
}
